package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SplitArrayOfLongValuesIntoListOfLong {
	public static void main(String[] argv) {
		long[] offsetArray = new long[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int numPerBlock = 42;
		List<long[]> longs = splitLongs(offsetArray, numPerBlock);
		for (long[] aLong : longs) {
			System.out.println(Arrays.toString(aLong));
		}
	}

	public static Random random = new Random();

	public static List<long[]> splitLongs(long[] offsetArray, int numPerBlock) {
		List<long[]> result = new ArrayList<long[]>(offsetArray.length
				/ numPerBlock + 1);
		long[] tmp = Arrays.copyOf(offsetArray, offsetArray.length);
		int i = 0;
		for (; i < offsetArray.length / numPerBlock; i++) {
			result.add(Arrays.copyOfRange(tmp, i * numPerBlock, (i + 1)
					* numPerBlock));
		}
		if ((i) * numPerBlock < tmp.length) {
			result.add(Arrays.copyOfRange(tmp, i * numPerBlock, tmp.length));
		}
		return result;
	}

	public static int[] shuffle(int[] array) {

		for (int i = 0; i < array.length; i++) {
			int randomPosition = random.nextInt(array.length);
			int temp = array[i];
			array[i] = array[randomPosition];
			array[randomPosition] = temp;
		}

		return array;
	}

	public static void shuffle(long[] array, int startIndex, int endIndex) {
		assert (endIndex <= array.length && startIndex >= 0 && endIndex > 0);
		for (int i = startIndex; i < endIndex; i++) {
			int randomPosition = random.nextInt(endIndex - startIndex)
					+ startIndex;
			long temp = array[i];
			array[i] = array[randomPosition];
			array[randomPosition] = temp;
		}

	}
}
